package ar.com.arkios.kfconsap.model;

import ar.com.arkios.kfconsap.excepciones.*;


import com.sap.mw.jco.JCO;

/**
 * @author Ale
 * @since 10-10-08
 */
public abstract class SimpleRFC {

	protected String bapiName;

	protected JCO.Function function;

	protected ClienteSAP clienteSAP;

	public SimpleRFC(ClienteSAP cliente, String bapi) {
		clienteSAP = cliente;
		bapiName = bapi;
	}

	/**
	 * Method createFunction. Crea la representacion de la BAPI.
	 * @throws BAPINoEncontradaException
	 */
	public void createFunction() throws BAPINoEncontradaException {

		if (this.getFunction() == null) {

			this.setFunction(
				clienteSAP
					.getRepository()
					.getFunctionTemplate(
						this.getBapiName().toUpperCase().trim())
					.getFunction());

			if (this.getFunction() == null) {
				throw new BAPINoEncontradaException();
			}
		}
	}



	/**
	 * Method ejecutar. Ejecuta la BAPI sin ningun parametro.
	 * @throws BAPINoEncontradaException
	 */
	public void ejecutar() throws BAPINoEncontradaException, BAPIException {
		createFunction();
		invocar();
	}

	/**
	 * Method invocar.
	 */
	protected void invocar()
	{
		clienteSAP.getConnection().execute(function);
	}
	
	public static void main(String[] args) {
	}
	/**
	 * Returns the function.
	 * @return JCO.Function
	 */
	public JCO.Function getFunction() {
		return function;
	}

	/**
	 * Sets the function.
	 * @param function The function to set
	 */
	public void setFunction(JCO.Function function) {
		this.function = function;
	}

	/**
	 * Returns the bapiName.
	 * @return String
	 */
	public String getBapiName() {
		return bapiName;
	}
}
